import Vue from "vue";
import App from "./App";
import store from "./store";
import utils from "common/utils";
import allrequest from "api/allrequest.js";
import uView from "@/uview-ui";
import http from "common/http";
import mixin from "./common/mixin";
import VueI18n from "vue-i18n";
import { Emit } from "./common/eventBus";
// #ifdef H5
import wechat from "./common/wechat.js";
Vue.prototype.$wechat = wechat;
if (utils.is_webpage()) {
  wechat.initJssdk();
}

if (process.env.NODE_ENV === "development") {
  // 本地调试使用
  const vconsole = require("vconsole");
  Vue.prototype.$vconsole = new vconsole(); // 使用vconsole
}

// #endif
// import { Language } from "./static/js/common.js";
Vue.config.productionTip = false;
Vue.config.ignoredElements.push("wx-open-launch-weapp");
Vue.config.ignoredElements.push("wx-open-subscribe");
Vue.prototype.$store = store;
Vue.prototype.$utils = utils;
Vue.prototype.$http = http;
Vue.prototype.$allrequest = allrequest;
Vue.prototype.Emit = Emit;

import footNav from "./components/diyNew/footNav/index.vue";
import transitionPage from "./components/transitionPage/index.vue";
import getSuccess from "./components/coupon/getSuccessPopup.vue";
import singleHeader from "./components/diySystemPage/components/singleHeader/index.vue";
Vue.component("singleHeader", singleHeader);
Vue.component("footNav", footNav);
Vue.component("transitionPage", transitionPage);
Vue.component("getSuccess", getSuccess);
Vue.use(VueI18n);
Vue.mixin({
  methods: {
    setData: function (obj, callback) {
      let that = this;
      const handleData = (tepData, tepKey, afterKey) => {
        tepKey = tepKey.split(".");
        tepKey.forEach((item) => {
          if (tepData[item] === null || tepData[item] === undefined) {
            let reg = /^[0-9]+$/;
            tepData[item] = reg.test(afterKey) ? [] : {};
            tepData = tepData[item];
          } else {
            tepData = tepData[item];
          }
        });
        return tepData;
      };
      const isFn = function (value) {
        return typeof value == "function" || false;
      };
      Object.keys(obj).forEach(function (key) {
        let val = obj[key];
        key = key.replace(/\]/g, "").replace(/\[/g, ".");
        let front, after;
        let index_after = key.lastIndexOf(".");
        if (index_after != -1) {
          after = key.slice(index_after + 1);
          front = handleData(that, key.slice(0, index_after), after);
        } else {
          after = key;
          front = that;
        }
        if (front.$data && front.$data[after] === undefined) {
          Object.defineProperty(front, after, {
            get() {
              return front.$data[after];
            },
            set(newValue) {
              front.$data[after] = newValue;
              that.$forceUpdate();
            },
            enumerable: true,
            configurable: true
          });
          front[after] = val;
        } else {
          that.$set(front, after, val);
        }
      });
      // this.$forceUpdate();
      isFn(callback) && this.$nextTick(callback);
    }
  }
});
const Language = "zh-CN";
// const Language = 'en'
const i18n = new VueI18n({
  locale: Language,
  messages: {
    "zh-CN": require("./static/lang/zh-CN.json"),
    en: require("./static/lang/en.json")
  }
});
Vue.prototype._i18n = i18n;
Vue.use(uView);
Vue.use(mixin);
App.mpType = "app";

async function setDefault() {
  var res = await allrequest.publicMethod.defaultStore({});
  if (res.code == 0) {
    store.commit("setStores", res.data.store);
    store.commit("setImages", res.data.images);
    store.commit("setShareSetting", res.data.share_setting);
    store.commit("setOneStoreSwitchStore", res.data.one_store_switch_store);
    store.commit("setOneStoreHideApply", res.data.one_store_hide_apply);
    store.commit("setPlatformAppid", res.data.platform_appid);
    store.commit("setCopyright", res.data.copyright);
    uni.setStorageSync("shareHolderAliasName", res.data.shareHolderAliasName);
    if (res.data.language && res.data.language != "") {
      i18n.locale = res.data.language;
    }

    // #ifdef MP-ALIPAY
    if (res.getClientConfig.ali_mini_plugin_goodsDetail == 1) {
      try {
        my.loadPlugin({
          plugin: "2021003177653028@*", // 指定要加载的插件 ID 和版本号，为 * 时自动选择版本
          success: () => {
            const plugin = requirePlugin("dynamic-plugin://2021003177653028");

            plugin.setPlaceOrderCallback((arg) => {
              let { outItemId, outSkuId, count } = arg;

              my.redirectTo({
                url: `/order/order-submit/order-submit?outItemId=${outItemId}&outSkuId=${outSkuId}&count=${count}` // 跳转到小程序的下单页面地址
              });
            });
          }
        });
      } catch (error) {}
    }
    // #endif
  }
}
// 获取控件模板
async function getModules() {
  try {
    const res = await allrequest.diyNew.getDiyIndexPage();
    if (res.code == 0 && res.data.controls) {
      if (res.data.controls && res.data.controls != "") {
        res.data.controls = JSON.parse(res.data.controls);
        store.commit("setControls", res.data.controls);
      }
    }
  } catch (e) {}
}

// 是否审核
(async () => {
  try {
    const res = await allrequest.request.mpAudit();
    // 是否审核
    store.commit("setchange_shop", res.data.change_shop);
    store.commit("setShenheStatus", res.data.status);
    store.commit("setShenheAliStatus", res.data.is_ali_shenhe);
  } catch (error) {}
})();
var var_ = setDefault();
if (store.getters.has_is_one_store) {
  const storeId = uni.getStorageSync("store_id");
  const userinfo = uni.getStorageSync("userInfo");
  if (
    !storeId &&
    userinfo &&
    userinfo.store_id &&
    parseInt(userinfo.store_id) > 0
  ) {
    // uni.setStorageSync("store_id", userinfo.store_id);
    store.commit("setStoreId", userinfo.store_id);
  }
  if (storeId || userinfo) {
    var var__ = getModules();
  }
} else {
  var var__ = getModules();
}

const app = new Vue({
  ...App,
  store,
  i18n
});
app.$mount();

// #ifdef WEB
// 解决三方图片不显示问题 no-referrer方案
(function () {
  const oMeta = document.createElement("meta");
  oMeta.name = "referrer";
  oMeta.content = "no-referrer";
  document.getElementsByTagName("head")[0].appendChild(oMeta);
})();
// #endif
